//
// Created by xbm on 23-3-2.
//
#include "logging.h"

#include <time.h>
#include <sstream>

#include <chrono>
#include "fmt/format.h"
#include "fmt/chrono.h"

using namespace std;
using namespace chrono;
using namespace logging;

void Logger::log(string_view sv) {
    log(sv, log_level_);
}

void Logger::log(string_view sv, LogLevel logLevel) {
    writer.write(fmt::format("{}  {:<7}  {}\n", get_formatted_time(), get_status_msg(logLevel), sv));
}

Logger::Logger(const string &filename, LogLevel log_level) :
        filename_(filename),
        log_level_(log_level),
        writer(filename) {}

string Logger::get_formatted_time() {
    auto now = fmt::localtime(system_clock::to_time_t(system_clock::now()));
    return fmt::format("{:%F %T}", now);
}

string Logger::get_status_msg(LogLevel logLevel) {
    switch (logLevel) {
        using
        enum LogLevel;
        case MESSAGE:
            return "message";
        case STATUS:
            return "status";
        case WARNING:
            return "warning";
        case ERROR:
            return "error";
        case FATAL:
            return "fatal";
        default:
            return "unknown";
    }
}


